
//if (typeof (jQuery) == "undefined") {
//    var script = document.createElement("script");
//    script.type = "text/javascript";
//    script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";


//    var cb = document.createElement("script");
//    cb.type = "text/javascript";
//    cb.textContent = "jQuery.noConflict();(" + autolog.toString() + ")(jQuery);";
//    script.addEventListener('load', function () {
//        document.head.appendChild(cb);
//    });
//}

//login
var url = "https://dynamic.12306.cn/otsweb/loginAction.do?method=login";
var queryurl = "https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init";
function submitForm() {
    var submitUrl = url;
    $.ajax({
        type: "POST",
        url: submitUrl,
        data: {
            "loginUser.user_name": $("#UserName").val()
				  , "user.password": $("#password").val()
				  , "randCode": $("#randCode").val()
        },
        timeout: 30000,
        //cache: false,
        //async: false,
        success: function (msg) {
            if (msg.indexOf('请输入正确的验证码') > -1) {
                alert('请输入正确的验证码！');
            };
            if (msg.indexOf('当前访问用户过多') > -1) {
                reLogin();
            }
            else {
                $('#refreshButton').html("成功,请点车票预订!");
                location.replace(queryurl);
            };
        },
        error: function (msg) {
            reLogin();
        },
        beforeSend: function (XHR) {
            //alert("Data Saved: " + XHR);
        }
    });
};

var count = 1;
function reLogin() {
    count++;
    $('#refreshButton').html("(" + count + ")次登录中...");
    setTimeout(submitForm, 2000);
};

//初始化
if ($("#refreshButton").size() < 1) {
    $("#subLink").after($("<a href='#' style='padding: 5px 10px; background: #2CC03E;border-color: #259A33;border-right-color: #2CC03E;border-bottom-color:#2CC03E;color: white;border-radius: 5px;text-shadow: -1px -1px 0 rgba(0, 0, 0, 0.2);'/>").attr("id", "refreshButton").html("自动登录").click(function () {
        alert('开始尝试登录，请点确定后耐心等待！');
        count = 1;
        $(this).html("(1)次登录中...");
        submitForm();
        return false;
    }));
    alert('如果使用自动登录功能，请输入用户名、密码及验证码后，点击自动登录，系统会尝试登录，直至成功！');
}